let customeraddModule = (function () {
    let $username = $('.username'),
        $spanusername = $('.spanusername'),
        $man = $('#man'),
        $woman = $('#woman'),
        $useremail = $('.useremail'),
        $spanuseremail = $('.spanemail'),
        $userphone = $('.userphone'),
        $spanuserphone = $('.spanphone'),
        $userqq = $('.userqq'),
        $userweixin = $('.userweixin'),
        $usertype = $('.usertype'),
        $userAddress = $('.userAddress'),
        $submit = $('.submit')
    let userid =null

    // 表单校验
    function checkName() {
        let val = $username.val().trim()
        if (val.length === 0) {
            $spanusername.html('当前是必填项')
            return false
        }
        if (!/^[\u4E00-\u9FA5]{2,10}(·[\u4E00-\u9FA5]{2,10}){0,2}$/.test(val)) {
            $spanusername.html('请填写真实姓名（2-10个汉字）')
            return false
        }
        $spanusername.html('')
        return true
    }
    function checkEmail() {
        let val = $useremail.val().trim()
        if (val.length === 0) {
            $spanuseremail.html('当前是必填项')
            return false
        }
        if (!/^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(val)) {
            $spanuseremail.html('请填写真实Email')
            return false
        }
        $spanuseremail.html('')
        return true
    }
    function checkPhone() { 
        let val = $userphone.val().trim() 
        if (val.length === 0) {
            $spanuserphone.html('当前是必填项')
            return false
        } 
        if (!/^1\d{10}$/.test(val)) {
            $spanuserphone.html('请填写真实手机号')
            return false
        }
        $spanuserphone.html('')
        return true
    }

    async function submitHandle() {
        if (!checkName() || !checkEmail() || !checkPhone()) return  
        // 获取用户输入的信息
        let params = {
            name: $username.val().trim(),
            sex: $man.prop('checked') ? 0 : 1,
            email:$useremail.val().trim(),
            phone: $userphone.val().trim(),
            QQ: $userqq.val().trim(),
            weixin: $userweixin.val().trim(),
            type: $usertype.val(),
            address:$userAddress.val().trim() 
        } 
        // 向服务器发送请求 (区分新增和修改)
        if (userid) {
            params.customerId = userid 
            let result = await axios.post('/customer/update', params)
            if (result.code == 0) {
                alert('修改成功！')
                window.location.href = 'customerlist.html'
                return;
            }
            alert('当前网络繁忙，请刷新重试~~')
            return
        }
        let result = await axios.post('/customer/add', params)   
        if (result.code == 0) {
            alert('添加成功！')
            window.location.href = 'customerlist.html'
            return;
        }
        alert('当前网络繁忙，请刷新重试~~')

        window.location.href = 'customerlist.html'
    }

    async function queryInfo() {
        let result = await axios.get('/customer/info', { params: {customerId:userid}})
        if (result.code == 0) {
            let {name,sex,email,phone,QQ,weixin,type,address} =result.data
            $username.val(name)
            sex==0? $man.prop('checked',true) : $woman.prop('checked',true)
            $useremail.val(email)
            $userphone.val(phone)
            $userqq.val(QQ)
            $userweixin.val(weixin)
            $usertype.val(type)
            $userAddress.val(address)  
            return
        } 
    }
    return {
        init() { 
            // 判断是新增还是修改
            let params = window.location.href.queryURLParams()
            if (params.hasOwnProperty('userid')) {
                userid = params.userid
                queryInfo()
            }

            $username.blur(checkName)
            $useremail.blur(checkEmail)
            $userphone.blur(checkPhone)
            // 提交
            $submit.click(submitHandle)

        }
    }
})()
customeraddModule.init()